package src;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dream
 * @date 2020/12/20 22:01
 */
public class MergeArray {
    /**
     * 数组的长度
     */
    private final int ARRAY_SIZE = 100;
    /**
     * 有几个数组合并
     */
    private final int ARRAY_COUNT = 5;
    /**
     * 总的大数组
     */
    private final int[] localArray = new int[ARRAY_SIZE * ARRAY_COUNT - 10];
    /**
     * 该数组已取的值的标记
     */
    private final int[] arrayIndex;
    /**
     * 合并时用的缓存堆
     */
    private Heap heap;
    /**
     * key 是数组
     * value 是值
     * 使用时遍历五个数组，找到对应的那个值。
     * 避免值相同的情况
     */
    private final Map<Integer, Integer> map = new HashMap<>();
    /**
     * 存放数组的集合
     */
    private final ArrayList<int[]> arrayList = new ArrayList<int[]>();

    public MergeArray() {
        for (int i = 0; i < ARRAY_COUNT; i++) {
            arrayList.add(createArray(i + 2));
        }
        //初始值默认都是 0
        arrayIndex = new int[arrayList.size()];
    }

    /**
     * @return 一个随机数组
     */
    private int[] createArray(int multiple) {
        int[] array = new int[ARRAY_SIZE];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) Math.round(multiple * (i + 1));
        }
        return array;
    }

    public void merge() {
        int[] temp = new int[arrayList.size()];
        //初始化
        for (int i = 0; i < ARRAY_COUNT; i++) {
            temp[i] = arrayList.get(i)[arrayIndex[i]++];
            map.put(i, arrayList.get(i)[0]);
        }
        heap = new Heap(temp);
        //对总的进行排序
        heap.heapDdescendingSort();
        //当数组还没满
        for (int i = 0; i < ARRAY_SIZE * ARRAY_COUNT - 10; i++) {
            int j = addArray();
            localArray[i] = j;
        }
    }

    /**
     * 插入大数组
     */
    //TODO
    private int addArray() {
        int removeValue = heap.getHead();
        int value;
        //找出这个值在哪个数组
        for (int i = 0; i < arrayIndex.length; i++) {
            if (removeValue == map.get(i)) {
                value = findValue(i, 1);
                //将新的值插入
                heap.setHead(value);
                //排序
//                heap.ascendingSort(0);
                heap.heapDdescendingSort();
                return value;
            }
        }
        return -1;
    }

    //TODO 测试

    /**
     * @param whichArray 哪个数组
     * @param count      传入的1是固定值。调用的时候必须传入 1
     * @return 数组（文件）里的值
     */
    private int findValue(int whichArray, int count) {
        if (count == ARRAY_COUNT) {
            return -1;
        }
        if (whichArray == ARRAY_COUNT) {
            whichArray = 0;
        }
        if (arrayIndex[whichArray] != ARRAY_SIZE - 1) {
            int value = arrayList.get(whichArray)[arrayIndex[whichArray]++];
            map.put(whichArray, value);
            return value;
        } else {
            return findValue(whichArray + 1, count + 1);
        }
    }

    public int[] getLocalArray() {
        return localArray;
    }
}